package gui.dialog;

import gui.extra.EpisodeLibraryPanel;
import gui.extra.MemoryFolderChooser;
import gui.extra.MovieLibraryPanel;
import gui.extra.VideoLibraryPanel;
import gui.extra.progress.StatusMessenger;
import gui.workers.TypedRenameWorker;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.border.EmptyBorder;

import lib.model.VideoType;
import settings.model.LibrarySettings;
import settings.model.MappingBierdopjeScene;
import settings.model.Settings;

public class RenameDialog extends JBierSubDialog implements PropertyChangeListener {

    /**
     *
     */
    private static final long serialVersionUID = 1L;
    private VideoLibraryPanel pnlLibrary;
    private JTextField txtRenameLocation;
    private JCheckBox chkRecursive;
    private List<MappingBierdopjeScene> listTranslate = new ArrayList<MappingBierdopjeScene>();
    private ProgressDialog progressDialog;

    /**
     * Create the dialog.
     */
    public RenameDialog(JFrame frame, final Settings settings, final VideoType videoType) {
        super(frame, videoType + " Hernoemen ...", false);
        listTranslate = settings.getMappingList();
        setResizable(false);
        setBounds(100, 100, 650, 680);
        getContentPane().setLayout(new BorderLayout());
        JPanel contentPanel = new JPanel();
        contentPanel.setBorder(new EmptyBorder(5, 5, 5, 5));
        getContentPane().add(contentPanel, BorderLayout.CENTER);
        GridBagLayout gbl_contentPanel = new GridBagLayout();
        gbl_contentPanel.columnWidths = new int[]{0, 0};
        gbl_contentPanel.rowHeights = new int[]{0, 0, 0};
        gbl_contentPanel.columnWeights = new double[]{1.0, Double.MIN_VALUE};
        gbl_contentPanel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
        contentPanel.setLayout(gbl_contentPanel);
        {
            JPanel panel = new JPanel();
            GridBagConstraints gbc_panel = new GridBagConstraints();
            gbc_panel.insets = new Insets(0, 0, 5, 0);
            gbc_panel.fill = GridBagConstraints.BOTH;
            gbc_panel.gridx = 0;
            gbc_panel.gridy = 0;
            contentPanel.add(panel, gbc_panel);
            GridBagLayout gbl_panel = new GridBagLayout();
            gbl_panel.columnWidths = new int[]{208, 151, 151, 0};
            gbl_panel.rowHeights = new int[]{40, 0, 0};
            gbl_panel.columnWeights = new double[]{0.0, 0.0, 0.0,
                    Double.MIN_VALUE};
            gbl_panel.rowWeights = new double[]{0.0, 0.0, Double.MIN_VALUE};
            panel.setLayout(gbl_panel);
            {
                JLabel lblFolderToRenamemove = new JLabel(
                        "Map voor Hernoemen/Verplaatsen");
                GridBagConstraints gbc_lblFolderToRenamemove = new GridBagConstraints();
                gbc_lblFolderToRenamemove.fill = GridBagConstraints.BOTH;
                gbc_lblFolderToRenamemove.insets = new Insets(0, 0, 5, 5);
                gbc_lblFolderToRenamemove.gridx = 0;
                gbc_lblFolderToRenamemove.gridy = 0;
                panel.add(lblFolderToRenamemove, gbc_lblFolderToRenamemove);
            }
            {
                JButton btnBrowser = new JButton("Bladeren");
                btnBrowser.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        File path = MemoryFolderChooser.getInstance().selectDirectory(getContentPane(),
                                "Selecteer map voor Hernoemen/Verplaatsen");
                        txtRenameLocation.setText(path.getAbsolutePath());
                    }
                });
                {
                    txtRenameLocation = new JTextField();
                    GridBagConstraints gbc_txtRenameLocation = new GridBagConstraints();
                    gbc_txtRenameLocation.fill = GridBagConstraints.HORIZONTAL;
                    gbc_txtRenameLocation.insets = new Insets(0, 0, 5, 5);
                    gbc_txtRenameLocation.gridx = 1;
                    gbc_txtRenameLocation.gridy = 0;
                    panel.add(txtRenameLocation, gbc_txtRenameLocation);
                    txtRenameLocation.setColumns(15);
                }
                GridBagConstraints gbc_btnBrowser = new GridBagConstraints();
                gbc_btnBrowser.insets = new Insets(0, 0, 5, 0);
                gbc_btnBrowser.gridx = 2;
                gbc_btnBrowser.gridy = 0;
                panel.add(btnBrowser, gbc_btnBrowser);
            }
            {
                chkRecursive = new JCheckBox("Mappen in map doorzoeken");
                GridBagConstraints gbc_chkRecursive = new GridBagConstraints();
                gbc_chkRecursive.insets = new Insets(0, 0, 0, 5);
                gbc_chkRecursive.gridx = 1;
                gbc_chkRecursive.gridy = 1;
                panel.add(chkRecursive, gbc_chkRecursive);
            }
        }
        {
            if (videoType == VideoType.EPISODE){
                pnlLibrary = new EpisodeLibraryPanel(settings.getEpisodeLibrarySettings());
            }else{
                pnlLibrary = new MovieLibraryPanel(settings.getMovieLibrarySettings());
            }
            
            GridBagConstraints gbc_panel = new GridBagConstraints();
            gbc_panel.fill = GridBagConstraints.BOTH;
            gbc_panel.gridx = 0;
            gbc_panel.gridy = 1;
            contentPanel.add(pnlLibrary, gbc_panel);
        }
        {
            JPanel buttonPane = new JPanel();
            buttonPane.setLayout(new FlowLayout(FlowLayout.RIGHT));
            getContentPane().add(buttonPane, BorderLayout.SOUTH);
            {
                JButton renameButton = new JButton("Hernoem");
                renameButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        ArrayList<MappingBierdopjeScene> list = new ArrayList<MappingBierdopjeScene>();
                        
                        if(videoType == VideoType.EPISODE ){
                            list = (ArrayList<MappingBierdopjeScene>) listTranslate;
                            rename(new File(txtRenameLocation.getText()),
                                    new File(txtRenameLocation.getText()),
                                    settings, pnlLibrary.getLibrarySettings(), list);
                        }else{
                            rename(new File(txtRenameLocation.getText()),
                                    new File(txtRenameLocation.getText()),
                                    new Settings(), pnlLibrary.getLibrarySettings(), list);
                        }
                        
                        setVisible(false);
                    }
                });
                renameButton.setActionCommand("Rename");
                buttonPane.add(renameButton);
                getRootPane().setDefaultButton(renameButton);
            }
            {
                JButton cancelButton = new JButton("Annuleren");
                cancelButton.addActionListener(new ActionListener() {
                    public void actionPerformed(ActionEvent arg0) {
                        setVisible(false);
                    }
                });
                cancelButton.setActionCommand("Cancel");
                buttonPane.add(cancelButton);
            }
        }
    }

    /**
     * 
     * @param dir
     * @param basedir
     * @param settings
     * @param librarySettings can be different from the store librarySettings
     * @param list
     */
    protected void rename(File dir, File basedir, Settings settings, LibrarySettings librarySettings, ArrayList<MappingBierdopjeScene> list) {
        TypedRenameWorker renameWorker = new TypedRenameWorker(dir,
                basedir, settings, librarySettings, VideoType.EPISODE, this.chkRecursive.isSelected());
        renameWorker.addPropertyChangeListener(this);
        progressDialog = new ProgressDialog(renameWorker);
        progressDialog.setVisible(true);
        renameWorker.execute();
    }

    @Override
    public void propertyChange(PropertyChangeEvent event) {
        if (event.getSource() instanceof TypedRenameWorker) {
            final TypedRenameWorker renameWorker = (TypedRenameWorker) event.getSource();
            if (renameWorker.isDone()) {
                progressDialog.setVisible(false);
            } else {
                final int progress = renameWorker.getProgress();
                progressDialog.updateProgress(progress);
                StatusMessenger.instance.message("Hernoemen ....");
            }
        }
    }
}

